package stack;

/***
 * 使用泛型实现栈
 * */
public class LinkedStack<T> {

    private Node<T> top = new Node<>();

    private static class Node<U>{
        U item;
        Node<U> next;
        Node(){
            item = null;
            next = null;
        }
        Node(U item,Node<U> next){
            this.item = item;
            this.next = next;
        }
        boolean end(){
            return item==null && next==null;
        }
    }

    public void push(T t){
        top = new Node<>(t,top);
    }

    public T pop(){
        T result = top.item;
        if(!top.end()){
            top = top.next;
        }
        return result;
    }


    public static void main(String[] args) {
        LinkedStack<String> ls = new LinkedStack<>();
        ls.push("李");
        ls.push("博");
        ls.push("飞");
        String s ;
        while((s = ls.pop())!=null){
            System.out.println(s);
        }
    }
}
